package com.seari.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.seari.bean.AlarmHistoryData;
import com.seari.bean.AlarmQuery;
import com.seari.bean.ETMaintainRecord;
import com.seari.pojo.DataGridBean;
import com.seari.pojo.QueryMtRecord;
import com.seari.service.AlertManagerService;
import com.seari.utils.StringHelper;

/**
 * @author TOUC 报警管理
 */
@Controller()
@RequestMapping("/alertManager")
public class AlertManagerController {

	@Autowired
	private AlertManagerService alertManagerService;

	/**
	 * @return 报警综合信息列表
	 */
	@RequestMapping(value = "/alarmSynMsg")
	@ResponseBody
	public DataGridBean<AlarmHistoryData> alarmSynMsg(
			@RequestBody(required = false) AlarmQuery alarmQuery) {

		DataGridBean<AlarmHistoryData> grid = new DataGridBean<AlarmHistoryData>();
		
		AlarmQuery aq = new AlarmQuery();

		if (alarmQuery != null) {

			int pageSize = alarmQuery.getPageSize();
			aq.setStart((alarmQuery.getPageNo() - 1) * pageSize);
			aq.setLimit(pageSize);

			List<AlarmHistoryData> ahdList = alertManagerService
					.getAlarmHisMsgs(aq);

			grid.setMsg(StringHelper.MESSAGE_TYPE_SUCCESS);
			grid.setRows(ahdList);
			grid.setTotal(alertManagerService.getHisMsgCount(aq));
		}

		return grid;

	}

	/**
	 * @return 查询报警综合信息
	 */
	@RequestMapping(value = "/queryAlarmSynMsg")
	@ResponseBody
	public DataGridBean<AlarmHistoryData> queryAlarmSynMsg(
			@RequestBody(required = false) AlarmQuery alarmQuery) {

		DataGridBean<AlarmHistoryData> grid = new DataGridBean<AlarmHistoryData>();

		if (alarmQuery != null) {

			int pageSize = alarmQuery.getPageSize();
			alarmQuery.setStart((alarmQuery.getPageNo() - 1) * pageSize);
			alarmQuery.setLimit(pageSize);

			List<AlarmHistoryData> ahdList = alertManagerService
					.getAlarmHisMsgs(alarmQuery);

			grid.setMsg(StringHelper.MESSAGE_TYPE_SUCCESS);
			grid.setRows(ahdList);
			grid.setTotal(alertManagerService.getHisMsgCount(alarmQuery));

		} 
		return grid;

	}

	/**
	 * @return 故障维修记录
	 */
	@RequestMapping(value = "/breakdownMtRecord")
	@ResponseBody
	public DataGridBean<ETMaintainRecord> breakdownMtRecord(
			@RequestParam(required = false) String page,
			@RequestParam(required = false) String limit) {

		DataGridBean<ETMaintainRecord> grid = new DataGridBean<ETMaintainRecord>();

		AlarmQuery aq = new AlarmQuery();
		if (StringUtils.isNotBlank(page) && StringUtils.isNotBlank(limit)) {
			aq.setStart((Integer.parseInt(page) - 1) * Integer.parseInt(limit));
			aq.setLimit(Integer.parseInt(limit));
		}

		grid.setMsg(StringHelper.MESSAGE_TYPE_SUCCESS);
		List<ETMaintainRecord> ETList = alertManagerService
				.getBreakdownMtRecord(aq);
		grid.setRows(ETList);
		grid.setTotal(alertManagerService.getBMRCount());
		return grid;

	}
	
	
	
	/**
	 * @return 查询设备维修记录
	 */
	@RequestMapping(value = "/queryMtRecord")
	@ResponseBody
	public DataGridBean<ETMaintainRecord> queryMtRecord(@RequestBody QueryMtRecord queryMtRecord) {

		DataGridBean<ETMaintainRecord> grid = new DataGridBean<ETMaintainRecord>();
		
		if(queryMtRecord == null){
			grid.setMsg("表单数据不完整");
			return grid;
		}
		int pageNo = queryMtRecord.getPageNo();
		int pageSize = queryMtRecord.getPageSize();
		queryMtRecord.setStart((pageNo-1) * pageSize);
		queryMtRecord.setLimit(pageSize);
		
		
		String domain = null;
		String systemName = null;
		String eventType = null;
		
		try{
			domain = queryMtRecord.getDomain();
		}catch(Exception e){
			
		}
		try{
			systemName = queryMtRecord.getSystemName();
		}catch(Exception e){
			
		}
		try{
			eventType = queryMtRecord.getEventType();
		}catch(Exception e){
			
		}
		if(domain != null)
			queryMtRecord.setDomain(alertManagerService.queryRemarkName(domain));
		if(systemName != null)
			queryMtRecord.setSystemName(alertManagerService.queryRemarkName(systemName));
		if(eventType != null)
			queryMtRecord.setEventType(alertManagerService.queryRemarkName(eventType));
		
		List<ETMaintainRecord>ETList =  alertManagerService.queryMtRecord(queryMtRecord);
		grid.setRows(ETList);
		grid.setMsg(StringHelper.MESSAGE_TYPE_SUCCESS);
		grid.setTotal(alertManagerService.queryMtRecordCount(queryMtRecord));
		return grid;

	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

}
